| Objective | Complete |
|---|---|
| Create nodes and edges dataframes | |
| Build and customize a network HTMLwidget |
Now that the dataset is prepped for visualization, we must generate the edge dataframe and the node dataframe that will comprise our network graph
We can start by transforming our similarity matrix into an edge dataframe
The edge dataframe informs visNetwork:
from which node to which nodevalue (the thickness) of the edgetidy function from the broom package, which turns the messy output of built-in R functions into tidy dataframeslibrary(broom)
# Create edge dataframe.
hds_edges = tidy(hds_sim)
# Edges dataframe has to be named this way for visNetwork input.
colnames(hds_edges) = c("from", "to", "value")
head(hds_edges)# A tibble: 6 x 3
from to value
<fct> <fct> <dbl>
1 1 3 0.455
2 1 4 0.734
3 1 5 0.747
4 1 6 0.800
5 1 7 0.299
6 1 8 0.405
visNetwork must have an id columnfrom and to columns of the edges dataframe# Get unique nodes from edges dataframe and combine them
hds_nodes_from = data.frame(id = unique(hds_edges$from))
hds_nodes_to = data.frame(id = unique(hds_edges$to))
hds_nodes = rbind(hds_nodes_from,hds_nodes_to)
# Retain unique nodes in case nodes are repeated in `from` and `to` columns
hds_nodes = unique(hds_nodes)# Add color to the nodes dataframe based on stroke value from original dataframe
hds_small = select(hds_small, stroke) #<- we only need the target info
hds_small$id = rownames(hds_small)
# Merge nodes dataframe with the dataframe with Target value
hds_nodes = merge(hds_nodes, hds_small,
by = "id", all.x = TRUE) #<- merge() needs the `id` column to
# join the two dataframes
# Assign color to nodes based on the stroke value
hds_nodes$color = factor(hds_nodes$stroke, #<- create a factor
labels = c("orange", "darkblue"), #<- assign color
levels = c(1, 0)) # based on Target value
# Because we aligned colors based on the stroke column, we can drop it
hds_nodes = select(hds_nodes, c(id, color)) id color
1 1002 darkblue
2 1019 darkblue
3 1035 darkblue
4 1036 darkblue
5 1038 darkblue
6 1044 darkblue
| Objective | Complete |
|---|---|
| Create nodes and edges dataframes |
✔ |
| Build and customize a network HTMLwidget |
Share your thoughts and findings in the chat: